package com.example.idgenerate.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.idgenerate.entity.Result;
import com.example.idgenerate.entity.SeckillVoucher;
import com.example.idgenerate.entity.VoucherOrder;
import com.example.idgenerate.mapper.VoucherOrderMapper;
import com.example.idgenerate.service.ISeckillVoucherService;
import com.example.idgenerate.service.IVoucherOrderService;
import com.sun.deploy.ui.AboutDialog;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Random;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Slf4j
@Service
public class VoucherOrderServiceImpl extends ServiceImpl<VoucherOrderMapper, VoucherOrder> implements IVoucherOrderService {
    @Resource
    private ISeckillVoucherService seckillVoucherService;

    @Override
    @Transactional
    public Result seckillVoucher(Long voucherId) {
        // 1.查询优惠券
        SeckillVoucher voucher = seckillVoucherService.getById(voucherId);
        // 2.判断秒杀是否开始
        if (voucher.getBeginTime().isAfter(LocalDateTime.now())) {
            // 尚未开始
            return Result.fail("秒杀尚未开始！");
        }
        // 3.判断秒杀是否已经结束
        if (voucher.getEndTime().isBefore(LocalDateTime.now())) {
            // 尚未开始
            return Result.fail("秒杀已经结束！");
        }
        // 4.判断库存是否充足
        if (voucher.getStock() < 1) {
            // 库存不足
            return Result.fail("库存不足！");
        }
        //5，扣减库存
        boolean success = Ordinary(voucherId);
        //修改代码方案一
//        boolean success = VersionNumber(voucherId, voucher);
        //修改代码方案二
//        boolean success = VersionNumberTwo(voucherId, voucher);

        if (!success) {
            //扣减库存
            return Result.fail("库存不足！");
        }
        //6.创建订单
        VoucherOrder voucherOrder = new VoucherOrder();
        // 6.1.订单id
        Random ra = new Random();
        long orderId = ra.nextLong();
        voucherOrder.setId(orderId);
        // 6.2.用户id
        voucherOrder.setUserId(1L);
        // 6.3.代金券id
        voucherOrder.setVoucherId(voucherId);

        save(voucherOrder);

        return Result.ok(orderId);

    }

    /**
     * 扣库存
     */
    private boolean Ordinary(Long voucherId) {
        boolean success = seckillVoucherService.update()
                .setSql("stock= stock -1")
                .eq("voucher_id", voucherId).update();
        return success;
    }

    /**
     * 修改代码方案一
     */
    private boolean VersionNumber(Long voucherId, SeckillVoucher voucher) {
        boolean success = seckillVoucherService.update()
                .setSql("stock= stock -1") //set stock = stock -1
                .eq("voucher_id", voucherId).eq("stock", voucher.getStock()).update(); //where id = ？ and stock = ?
        return success;
    }

    /**
     * 修改代码方案二
     */
    private boolean VersionNumberTwo(Long voucherId, SeckillVoucher voucher) {
        boolean success = seckillVoucherService.update()
                .setSql("stock= stock -1")
                .eq("voucher_id", voucherId).gt("stock", 0).update(); //where id = ? and stock > 0
        return success;
    }
}
